﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

using System.Timers;
using System.Collections.ObjectModel;
using Cursors = System.Windows.Input.Cursors;

namespace ZhwangTool
{
    /// <summary>
    /// EmptyDirectoryRemoverUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class EmptyDirectoryRemoverUserControl : System.Windows.Controls.UserControl
    {
        public EmptyDirectoryRemoverUserControl()
        {
            InitializeComponent();
        }

        List<string> emptyDirs = new List<string>();

        private void Button11_Click(object sender, RoutedEventArgs e)
        {
            richTextBox1.Document.Blocks.Clear();

            emptyDirs.Clear();

            string givePath = string.Empty;
            
            if (Directory.Exists(textBox1.Text))
            {
                givePath = textBox1.Text;
            }

            if (Directory.Exists(givePath) ==false)
            {
                return;
            }

            if (checkBox1.IsChecked==true)
            {
                emptyDirs = GetEmptyFolders(givePath,SearchOption.AllDirectories);
            }
            else
            {
                emptyDirs = GetEmptyFolders(givePath, SearchOption.TopDirectoryOnly);
            }


            emptyDirs = GetEmptyFolders(givePath);

            FlowDocument fdoc = richTextBox1.Document;

            Paragraph paragraph = new Paragraph();

            if (emptyDirs.Count > 0)
            {
                Run runHead = new Run("路径中的空文件夹包括:"+ "\r\n");

                runHead.Foreground = Brushes.Blue;

                paragraph.Inlines.Add(runHead);


                foreach (var item in emptyDirs)
                {
                    Run run1 = new Run(item + "\r\n");

                    run1.Foreground = Brushes.Black;

                    paragraph.Inlines.Add(run1);

                    try
                    {
                        //Directory.Delete(item);
                    }
                    catch (Exception)
                    {
                        continue;
                        //throw;
                    }
                }
            }
            else
            {
                Run runHead = new Run("路径中没有找到空文件夹:" + "\r\n");

                runHead.Foreground = Brushes.Red;

                paragraph.Inlines.Add(runHead);

            }
            
            richTextBox1.Document.Blocks.Add(paragraph);

            richTextBox1.ScrollToEnd();

        }

        static List<string> GetEmptyFolders(string path)
        {
            List<string> emptyFolders = new List<string>();
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            FileSystemInfo[] fileSystemInfos = dirInfo.GetFileSystemInfos();
            foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
            {
                if (fileSystemInfo is DirectoryInfo)
                {
                    DirectoryInfo dir = fileSystemInfo as DirectoryInfo;

                    try
                    {
                        if (dir.GetFiles().Length == 0 && dir.GetDirectories().Length == 0)
                        {
                            emptyFolders.Add(dir.FullName);
                        }
                        emptyFolders.AddRange(GetEmptyFolders(dir.FullName));

                    }
                    catch (Exception)
                    {
                        continue;
                        //throw;
                    }



                }
            }
            return emptyFolders;
        }

        static List<string> GetEmptyFolders(string path,SearchOption searchOption)
        {
            List<string> emptyFolders = new List<string>();
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            FileSystemInfo[] fileSystemInfos = dirInfo.GetFileSystemInfos("*",searchOption);
            foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
            {
                if (fileSystemInfo is DirectoryInfo)
                {
                    DirectoryInfo dir = fileSystemInfo as DirectoryInfo;

                    try
                    {
                        if (dir.GetFiles().Length == 0 && dir.GetDirectories().Length == 0)
                        {
                            emptyFolders.Add(dir.FullName);
                        }
                        emptyFolders.AddRange(GetEmptyFolders(dir.FullName));

                    }
                    catch (Exception)
                    {
                        continue;
                        //throw;
                    }



                }
            }
            return emptyFolders;
        }



        private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void Button12_Click(object sender, RoutedEventArgs e)
        {
            FlowDocument fdoc = richTextBox1.Document;

            Paragraph paragraph = new Paragraph();

            if (emptyDirs.Count==0)
            {
                return;
            }

            foreach (var item in emptyDirs)
            {

                try
                {
                    Directory.Delete(item);

                    Run run1 = new Run(item + "已经删除" + "\r\n");

                    run1.Foreground = Brushes.DarkGreen;

                    paragraph.Inlines.Add(run1);
                }
                catch (Exception)
                {
                    Run run1 = new Run("发生意外，未能删除文件" + item + "\r\n");

                    run1.Foreground = Brushes.Red;

                    paragraph.Inlines.Add(run1);

                    continue;
                    //throw;
                }

            }

            richTextBox1.Document.Blocks.Add(paragraph);

            richTextBox1.ScrollToEnd();

            emptyDirs.Clear();

        }

        private void DataGrid2_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog()==DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
